package com.qzsf.backend.service.impl;

import com.qzsf.backend.common.Result;
import com.qzsf.backend.service.FileService;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/10/31 2:59 周四
 * @Email 1429278462@qq.com
 * @Remark 文件阿里oss交互实现类
 * @Version 1.0
 */
import com.qzsf.backend.util.AliOssUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Override
    public String uploadFile(MultipartFile file) throws Exception {
            String originalFileName = file.getOriginalFilename();
            String fileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.lastIndexOf("."));
            return AliOssUtil.uploadFile(fileName, file.getInputStream());
    }

    @Override
    public void deleteFile(String objectName) throws Exception {
        if (objectName.startsWith("http://") || objectName.startsWith("https://")) {
            // 提取出 URL 中最后一部分作为文件名
            objectName = objectName.substring(objectName.lastIndexOf("/") + 1);
        }
        AliOssUtil.deleteFile(objectName);
    }
}
